package com.nowcoder.community.controller;

import com.nowcoder.community.service.TestService;
import com.nowcoder.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @ClassName ControllerTest
 * @Description 这个类专门用于纯粹知识点的举例，和业务无关
 * @Author hhwu
 * @Date 2022/5/12 22:15
 * Version 1.0
 **/
@Controller
@RequestMapping("/test")
public class TestController {
    @RequestMapping("/hello")
    @ResponseBody
    public String test1() {
        return "hello!";
    }

    @Autowired
    private TestService testService;

    @RequestMapping("/data")
    @ResponseBody
    public String getData() {
        return testService.find();
    }

    @RequestMapping("/http")
    public void http(HttpServletRequest request, HttpServletResponse response) {
        //获取请求数据
        System.out.println(request.getMethod());
        System.out.println(request.getServletPath());
        Enumeration<String> enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            String value = request.getHeader(key);
            System.out.println("key: "+key+" value: "+value);
        }
        System.out.println(request.getParameter("code"));
    }

    @RequestMapping(path = "/tests", method = RequestMethod.GET)
    @ResponseBody
    public String getStudents(
            @RequestParam(name = "cur", required = false, defaultValue = "1") int current,
            @RequestParam(name = "lim", required = false, defaultValue = "2") int limit) {
        return "current: " + current + ";\n limit: " + limit + ".";
    }

////    接收表单传过来的数据
//    @RequestMapping(path = "/student", method = RequestMethod.POST)
//    @ResponseBody
//    public String saveStudent(String name, int age) {
//        System.out.println(name);
//        System.out.println(age);
//        return "name: " + name + "; age: " + age;
//    }

//    接收表单传过来的数据,并响应HTML数据
    @RequestMapping(path = "/student", method = RequestMethod.POST)
    public ModelAndView saveStudent(String name, String age) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("mes", "已完成对学生的响应，结果是：");
        mav.addObject("name", name);
        mav.addObject("age", age);
        mav.setViewName("/demo/view");
        return mav;
    }

    @RequestMapping(path = "/school", method = RequestMethod.GET)
    public String getSchool(Model model) {
        model.addAttribute("mes", "对学校的响应");
        model.addAttribute("name", "Peking University");
        model.addAttribute("age", 80);
        return "/demo/view";
    }

    // 响应JSON数据
    @RequestMapping(path = "/jsontest", method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String, String>> jsonTest() {
        List<Map<String, String>> list = new ArrayList<>();

        Map<String, String> map = new HashMap<>();
        map.put("name", "zs");
        map.put("age", "22");
        map.put("salary", "22");
        Map<String, String> map1 = new HashMap<>();
        map1.put("name", "zs");
        map1.put("age", "22");
        map1.put("salary", "22");
        Map<String, String> map2 = new HashMap<>();
        map2.put("name", "zs");
        map2.put("age", "22");
        map2.put("salary", "22");

        list.add(map);
        list.add(map1);
        list.add(map2);
        return list;
    }

    // cookie示例
    @RequestMapping(path = "/cookie/set", method = RequestMethod.GET)
    @ResponseBody// 方法的返回值类型无所谓，因为cookie是保存在相应头里的
    public String setCookie(HttpServletResponse response) {
        Cookie cookie = new Cookie("cookie", CommunityUtil.generateUUID());// 一个cookie中只能有一对key-value，value只能为字符串
        cookie.setPath("/community/test");// 设置cookie生效范围
        cookie.setMaxAge(60 * 10);// 单位为秒
        response.addCookie(cookie);// 响应携带cookie
        return "set cookie!";
    }

    // 服务器获取cookie
    @RequestMapping(path = "/cookie/get", method = RequestMethod.GET)
    @ResponseBody
    public String getCookie(@CookieValue("cookie") String cookie) {// 获取cookie中的值
        System.out.println(cookie);
        return "get cookie";
    }

    // session会存在服务器端，将session地址id存入cookie传给客户端，然后客户端通过cookie中的session id找到服务器中对应的session
    @RequestMapping(path = "/session/set", method = RequestMethod.GET)
    @ResponseBody
    public String setSession(HttpSession session) {
        session.setAttribute("id", 1);
        session.setAttribute("name", "Test");
        return "set session";
    }

    @RequestMapping(path = "/session/get", method = RequestMethod.GET)
    @ResponseBody
    public String getSession(HttpSession session) {
        System.out.println(session.getAttribute("id"));
        System.out.println(session.getAttribute("name"));
        return "get SESSION!";
    }

    @RequestMapping(path = "/ajax", method = RequestMethod.POST)
    @ResponseBody
    public String testAjax(String name, int age) {
        System.out.println(name);
        System.out.println(age);
        return CommunityUtil.getJSONString(0, "成功！");
    }
}
